package com.chenjiagou.controller;

import com.chenjiagou.pojo.User;
import com.chenjiagou.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @GetMapping("/register")
    public String showRegister() {
        return "register"; // 返回 templates/register.html 页面
    }

    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password,
                           @RequestParam String phone,
                           @RequestParam String email) {

        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password); // 注意：应加密存储密码（如使用 BCrypt）
        newUser.setPhone(phone);
        newUser.setEmail(email);
        newUser.setRole(0); // 默认角色
        newUser.setStatus(1);   // 默认启用状态
        newUser.setCreated_at(java.time.LocalDateTime.now().toString());

        boolean success = registerService.register(newUser);

        if (success) {
            return "redirect:/login";
        } else {
            return "register"; // 注册失败返回注册页
        }
    }
}
